﻿<cfsilent>
	<cfscript>

		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		datetimeAdvice = getProperty("serviceFactory").getBean("datetimeAdvice");

		markAdvice = getProperty("serviceFactory").getBean("markAdvice");
		sequenceStudentMark = getProperty("serviceFactory").getBean("sequenceStudentMark");


		response = structNew();
		
		structInsert(response, "success", false, true);

		teacherId = sessionAdvice.getAutherUserID();
		hashCode = event.getArg("Sign");
		
		chooserId = event.getArg("ChooseID");
		mark_t = event.getArg("Mark");
		markMode = event.getArg("MarkMode");
		markNote = event.getArg("MarkNote");
		states = event.getArg("State");
		
		taskId = event.getArg("TSK");
		
		
		try {
			
			if ( decrypt(hashCode, teacherId, "PBEWithMD5AndDES", "Hex") eq chooserId ) {
				
				transaction {
			
				
					
					/* 根据成绩备注矫正分数 */
					if ( markNote eq "2" ) { /* 2 - 缺考 */ mark_t = -1; }
					if ( markNote eq "3" ) { /* 3 - 缓考 */ mark_t = -1; }
					//if ( markNote eq "9" ) { /* 9 - 违纪 */ scroll = 0; }
			
					/* 根据成绩计分模式调整分数 */
					
					/*赛选调整分数类*/
					HSQL = "";
					switch(states){
						CASE 'u':
							HSQL = "tmm.mark_u = :mark ";
							break;
						CASE 'm':	
							HSQL = "tmm.mark_m = :mark ";
							break;
						CASE 'f':
							HSQL = "tmm.mark_f = :mark ";
							break;
					}
					
					/*更新临时成绩数据*/	
					sql ="	UPDATE 
								t_temp_mark tmm 
							SET 
								tmm.mark_mode = :markMode , 
								tmm.mark_note = :markNote , 
								" & HSQL & "
							WHERE 
								tmm.scc_id = :chooserId ";
							
					updateObj = new Query(datasource = application.dnsMaster);
					
					updateObj.addParam(name="markMode", value=markMode, cfsqltype="cf_sql_char");
					updateObj.addParam(name="markNote", value=markNote, cfsqltype="cf_sql_char");
					updateObj.addParam(name="mark", value=mark_t, cfsqltype="cf_sql_decimal", scale=2);
					updateObj.addParam(name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar");

					updateObj.execute(sql=sql);
					
					/*如果成绩完成，那么正式提交成绩*/
					
					sql = "	SELECT tmm.retaking,
								tmm.mark_mode,
								tmm.mark_u,
								tmm.mark_m,
								tmm.mark_f,
								tmm.mark_note,
								tmm.task_type,
								tmm.task_id,
								tmm.scc_id,
								tmm.stu_id,
								tmm.prop_id,
								c.cid,
								tmm.term_id,
								tmm.tch_id,
								c.course_credit
							FROM t_temp_mark tmm
								INNER JOIN t_course c ON c.cid = tmm.cid
							WHERE
								(
									(tmm.mark_note = '1' and tmm.mark_note_u = '1' and tmm.mark_note_m = '1' and tmm.mark_note_f = '1' and tmm.prop_id = '1' and (tmm.mark_u > 0 OR tmm.mark_u = 0) and (tmm.mark_m > 0 OR tmm.mark_m = 0) and (tmm.mark_f > 0 OR tmm.mark_f = 0))
									OR (tmm.mark_note = '1' and tmm.mark_note_u = '1' and tmm.mark_note_m = '1' and tmm.mark_note_f = '1' and tmm.prop_id = '2' and (tmm.mark_u > 0 OR tmm.mark_u = 0) and (tmm.mark_f > 0 OR tmm.mark_f = 0)) 
									OR (tmm.mark_mode = '4' AND (tmm.mark_f > 0 or tmm.mark_f = '0'))
									OR (tmm.mark_mode = '2' AND (tmm.mark_f > 0 or tmm.mark_f = '0'))
									OR (tmm.mark_note > '1' OR tmm.mark_note < '1') 
								)
								AND tmm.scc_id = :chooserId 
							ORDER BY
								tmm.stu_id ";
					
					queryObj = new Query(datasource = application.dnsMaster);
					
					queryObj.addParam(name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar");
					
					rs_mark = queryObj.execute(sql=sql).getResult();
					
					if( rs_mark.recordCount ){
						
						timeStamp = datetimeAdvice.getCurrentDatetimeString();
						
						sequenceStudentMark.setStudentID(rs_mark.stu_id);
					    sequenceStudentMark.setCourseID(rs_mark.cid);
					    sequenceStudentMark.setTermID(rs_mark.term_id);
					    sequenceStudentMark.setTaskType(rs_mark.task_type);
					    
					    markID = sequenceStudentMark.getID();
					    
					    sql = "	SELECT
								    t.scm_id
							    FROM
								    t_student_mark t
							    WHERE
								    t.scm_id = :markId ";

					    queryObj = new Query(datasource = application.dnsMaster);
					    
					    queryObj.addParam(name="markId", value=markID, cfsqltype="cf_sql_varchar");
					    
					    rs_check = queryObj.execute(sql=sql).getResult();
					    
					    if( rs_check.recordCount eq 0 ){

						    sql = "INSERT INTO t_student_mark
											 (scm_id,
											  stu_id,
											  term_id,
											  cid,
											  prop_id,
											  retaking,
											  mark_mode,
											  mark_result,
											  mark_note,
											  mark_resit,
											  mark_renote,
											  task_type,
											  task_id,
											  tch_id,
											  mark_time,
											  mark_resit_time,
											  scc_id,
											  mark_resit_limit,
											  remarks,
											  credit_point,
											  credit, 
											  credit_point_o,
											  mark_usual,
											  mark_middle, 
											  mark_final 
											  )
										    values
											 (:markID ,
											  :studentId ,
											  :termId ,
											  :courseId ,
											  :propId ,
											  :retaking ,
											  :markMode ,
											  :markResult ,
											  :markNote ,
											  :markResit ,
											  :markRenote ,
											  :taskType ,
											  :taskId ,
											  :teacherId ,
											  :markTime ,
											  :markResitTime ,
											  :chooserId ,
											  :markResitLimit,
											  :remarks, 
											  :creditPoint,
											  :credit, 
											  :creditPointO,
											  :markUsual,
											  :markMiddle, 
											  :markFinal )";
						    
					    }
					    else {
						    sql ="UPDATE t_student_mark t
									  SET 
										  t.stu_id= :studentId,
										  t.term_id = :termId,
										  t.cid= :courseId,
										  t.prop_id= :propId,
										  t.retaking= :retaking,
										  t.mark_mode= :markMode,
										  t.mark_result= :markResult,
										  t.mark_note= :markNote,
										  t.mark_resit= :markResit,
										  t.mark_renote= :markRenote,
										  t.task_type= :taskType,
										  t.task_id= :taskId,
										  t.tch_id= :teacherId,
										  t.mark_time= :markTime,
										  t.mark_resit_time= :markResitTime,
										  t.scc_id= :chooserId,
										  t.mark_resit_limit = :markResitLimit,
										  t.remarks = :remarks,
										  t.credit_point = :creditPoint,
										  t.credit = :credit, 
										  t.credit_point_o = :creditPointO, 
										  t.mark_usual = :markUsual, 
										  t.mark_middle = :markMiddle, 
										  t.mark_final = :markFinal 
									WHERE 
										  t.scm_id = :markID ";

					    }
					    
					    /* 根据备注规范 */
					    mark = 0;
				    
					    
					    /* 2 - 缺考 */
					    if ( rs_mark['mark_note'][1] eq "2" ) {
						    mark = -1;
					    }
					    
					    /* 3 - 缓考 */
					    if ( rs_mark['mark_note'][1] eq "3" ) {
						    mark = -1;
					    }
					    
					    /* 9 - 违纪 */
					    if ( rs_mark['mark_note'][1] eq "9" ) {
						    mark = -1;
					    }
					    
					    
					    /* 规范百分制成绩分数 */
					    if ( rs_mark['mark_mode'][1] eq "0" and rs_mark['mark_note'][1] eq "1" ){
						    
						    if( rs_mark['prop_id'][1] eq "1" ){ //必修
						    
							    mark = ( rs_mark['mark_u'][1] * 20 + rs_mark['mark_m'][1] * 30 + rs_mark['mark_f'][1] * 50 )/100;
							    
						    }else if ( rs_mark['prop_id'][1] eq "2" ){//选修
						    
							    mark = ( rs_mark['mark_u'][1] * 20 + rs_mark['mark_f'][1] * 80 )/100;
							    
						    }
					    }
					    
					    /* 规范二级制和五级制成绩分数 */
					    if ( (rs_mark['mark_mode'][1] eq "2" or rs_mark['mark_mode'][1] eq "4")  and rs_mark['mark_note'][1] eq "1" ) {
						    
							    mark = rs_mark['mark_f'][1];
						    
					    }
					    
					    courseCredit = rs_mark['course_credit'][1];
					    
					    
					    if( mark lt 60){
						    courseCredit = 0;	
					    }
					    
					    queryObj = new Query(datasource = application.dnsMaster);

					    queryObj.addParam(name="markID", value=markID, cfsqltype="cf_sql_varchar");
					    queryObj.addParam(name="studentId", value=rs_mark['stu_id'][1], cfsqltype="cf_sql_varchar");
					    queryObj.addParam(name="termId", value=rs_mark['term_id'][1], cfsqltype="cf_sql_char");
					    queryObj.addParam(name="courseId", value=rs_mark['cid'][1], cfsqltype="cf_sql_varchar");
					    queryObj.addParam(name="propId", value=rs_mark['prop_id'][1], cfsqltype="cf_sql_char");
					    queryObj.addParam(name="retaking", value=rs_mark['retaking'][1], cfsqltype="cf_sql_char");
					    queryObj.addParam(name="markMode", value=rs_mark['mark_mode'][1], cfsqltype="cf_sql_char");
					    queryObj.addParam(name="markResult", value=mark, cfsqltype="cf_sql_varchar");
					    queryObj.addParam(name="markNote", value=rs_mark['mark_note'][1], cfsqltype="cf_sql_char");
					    queryObj.addParam(name="markResit", value=-1, cfsqltype="cf_sql_varchar");
					    queryObj.addParam(name="markRenote", value=1, cfsqltype="cf_sql_char");
					    queryObj.addParam(name="taskType", value=rs_mark['task_type'][1], cfsqltype="cf_sql_char");
					    queryObj.addParam(name="taskId", value=rs_mark['task_id'][1], cfsqltype="cf_sql_varchar");
					    queryObj.addParam(name="teacherId", value=rs_mark['tch_id'][1], cfsqltype="cf_sql_varchar");
					    queryObj.addParam(name="markTime", value=timeStamp, cfsqltype="cf_sql_char");
					    queryObj.addParam(name="markResitTime", value=timeStamp, cfsqltype="cf_sql_char");
					    queryObj.addParam(name="chooserId", value=rs_mark['scc_id'][1], cfsqltype="cf_sql_varchar");
					    queryObj.addParam(name="markResitLimit", value=100, cfsqltype="cf_sql_decimal", scale=2);
					    queryObj.addParam(name="remarks", value="", cfsqltype="cf_sql_varchar");
					    queryObj.addParam( name="creditPoint", value=markAdvice.getMarkCredit(rs_mark['mark_mode'][1], rs_mark['course_credit'][1], mark, -1, 100), cfsqltype="cf_sql_decimal", scale=4 );
					    queryObj.addParam(name="credit", value=courseCredit, cfsqltype="cf_sql_integer");
					    queryObj.addParam(name="creditPointO", value=courseCredit * markAdvice.getMarkCredit(rs_mark['mark_mode'][1], rs_mark['course_credit'][1], mark, -1, 100), cfsqltype="cf_sql_varchar");
					    queryObj.addParam(name="markUsual", value=rs_mark['mark_u'][1], cfsqltype="cf_sql_decimal", scale=2);
					    queryObj.addParam(name="markMiddle", value=rs_mark['mark_m'][1], cfsqltype="cf_sql_decimal", scale=2);
					
					    queryObj.addParam(name="markFinal", value=rs_mark['mark_f'][1], cfsqltype="cf_sql_decimal", scale=2);
					    
					    queryObj.execute(sql=sql);	
					    
					    
					  
					}
					
					  /* 更新成绩提交数量，先查询数量 */
					  sql = "	SELECT 
								  count(tmm.scc_id)  mark_need,
								  SUM(
									  CASE
										  WHEN tmm.mark_note = '1' and tmm.mark_note_u = '1' and tmm.mark_note_m = '1' and tmm.mark_note_f = '1' and tmm.prop_id = '1' and (tmm.mark_u > 0 OR tmm.mark_u = 0) and (tmm.mark_m > 0 OR tmm.mark_m = 0) and (tmm.mark_f > 0 OR tmm.mark_f = 0) then
											  1
										  WHEN tmm.mark_note = '1' and tmm.mark_note_u = '1' and tmm.mark_note_m = '1' and tmm.mark_note_f = '1' and tmm.prop_id = '2' and (tmm.mark_u > 0 OR tmm.mark_u = 0) and (tmm.mark_f > 0 OR tmm.mark_f = 0) then
											  1
										  WHEN tmm.mark_mode = '4' AND (tmm.mark_f > 0 or tmm.mark_f = '0') then
											  1 
										  WHEN tmm.mark_mode = '2' AND (tmm.mark_f > 0 or tmm.mark_f = '0') then
											  1 
										  WHEN tmm.mark_note > '1' OR tmm.mark_note < '1' then
											  1
										  ELSE
											  0
										  END
									  )  mark_done
								  FROM t_temp_mark tmm 
								  WHERE tmm.task_id = :taskId ";		
					  queryObj = new Query(datasource = application.dnsMaster);
					  queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_char");
					  rs_task_mark = queryObj.execute(sql = sql).getResult();
								  
					  sql = "	UPDATE t_task_mark t
								  SET t.mark_need = :markNeed , t.mark_done = :markDone 
								  WHERE t.task_id = :taskId ";
					  updateObj = new Query(datasource = application.dnsMaster);
					  updateObj.addParam(name="markNeed", value=rs_task_mark.mark_need, cfsqltype="cf_sql_integer");
					  updateObj.addParam(name="markDone", value=rs_task_mark.mark_done, cfsqltype="cf_sql_integer");
					  updateObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
					  updateObj.execute(sql=sql);
					
					
					structInsert(response, "success", true, true);
					structInsert( response, "ChooseID",chooserId );
					structInsert( response, "Mark", numberFormat(mark_t, "_._") );
					
				}
				
			}
			else {
				structInsert(response, "reason", "验证数据失效", true);
			}
			
		}
		catch (any ecpt) {
				structInsert(response, "reason", "验证数据失效", true);
		}

	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>